//
//  LzwBnmCollectionReusableView.m
//  CoJellyCartoon
//
//  Created by 刘志武 on 16/4/1.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "LzwBnmCollectionReusableView.h"

@interface LzwBnmCollectionReusableView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *myScrollView;
@property (nonatomic, retain) UIImageView *myImageView;


@property (nonatomic, assign) NSInteger flag;//记录当前页码
@property (nonatomic , strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *myTimer;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger number;


@property (nonatomic, copy) NSString *string;

@end


@implementation LzwBnmCollectionReusableView

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(TimerAction:) userInfo:nil repeats:YES];

        //初始化数组
        self.arrayLb = [NSMutableArray array];
        
        [self creatHeaderView];
    }
    
    return self;
}

- (void)creatHeaderView{
    
    NSString *url = @"http://bangumi.bilibili.com/api/app_index_page_v2?_device=android&_hwid=e647d7e136a2c2a9&_ulv=10000&access_key=ac2771d488a63b7c3f8ccf926d8fe761&appkey=c1b107428d337928&build=414000&platform=android&ts=1459331038000&sign=10781fa9308c34870235f41b5b6cf643";
    
    [WZAFNetTool GetNetWithURL:url WithBody:nil headfile:nil response:WZJSON success:^(id reuslt) {
        
        NSMutableDictionary *dict = [reuslt objectForKey:@"result"];
        
#pragma mark -- 录播图数据
        NSMutableArray *arrayL = [dict objectForKey:@"banners"];
        
        for (NSMutableDictionary *dictL in arrayL) {
            
            LzwBnimationLBModel *modelLB = [[LzwBnimationLBModel alloc]init];
            
            [modelLB setValuesForKeysWithDictionary:dictL];
            
            [self.arrayLb addObject:modelLB];
            
        }
        
        [self creatSubViews];
        
    } failure:^(NSError *error) {
        
        
    }];
    
}


- (void)creatSubViews{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(30, 0.16 * SCREEN_HEIGHT, 150, 40);
    button.tag = 645001;
    [button setImage:[UIImage imageNamed:@"L1.png"] forState:UIControlStateNormal];
    [self addSubview:button];
    [button addTarget:self action:@selector(ButtonUpAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(200, 0.16 * SCREEN_HEIGHT, 150, 40);
    button1.tag = 645002;
    [button1 setImage:[UIImage imageNamed:@"L2.png"] forState:UIControlStateNormal];
    [self addSubview:button1];
    [button1 addTarget:self action:@selector(ButtonUpAction:) forControlEvents:UIControlEventTouchUpInside];

    self.myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.15 * SCREEN_HEIGHT)];
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.delegate = self;
    self.myScrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.myScrollView];
    
    for (int i = 0; i < self.arrayLb.count; i ++) {
        
        
        self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake((i * SCREEN_WIDTH) + SCREEN_WIDTH, 0, SCREEN_WIDTH, 0.15 * SCREEN_HEIGHT)];
        
        NSURL *url = [NSURL URLWithString:_arrayLb[i].img];
        
        
        [self.myImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"girl"]];
        self.myImageView.userInteractionEnabled = YES;
        
        //给图片添加轻拍手势
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ImageTapAction:)];
        
//        [self.myImageView addGestureRecognizer:tap];
        
        [self.myScrollView addSubview:self.myImageView];
        
        
        
    }
    
    
    if (self.arrayLb.count == 0) {
        return;
    }
    
    //取出最后一张放在最前面
    UIImageView *imageViewOne = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.15 * SCREEN_HEIGHT)];
    NSURL *urlOne = [NSURL URLWithString: [self.arrayLb objectAtIndex:self.arrayLb.count - 1].img];
    [imageViewOne sd_setImageWithURL:urlOne placeholderImage:[UIImage imageNamed:@"girl"]];
    [self.myScrollView addSubview:imageViewOne];
    
    //取出数组中第一张放在最后面
    UIImageView *imageTwo = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH * (self.arrayLb.count + 1), 0, SCREEN_WIDTH, 0.15 * SCREEN_HEIGHT)];
    NSURL *urlTwo = [NSURL URLWithString:[self.arrayLb objectAtIndex:0].img];
    [imageTwo sd_setImageWithURL:urlTwo placeholderImage:[UIImage imageNamed:@"girl"]];
    [self.myScrollView addSubview:imageTwo];
    
    id imageF = [self.arrayLb firstObject];
    id imageL = [self.arrayLb lastObject];
    
    //取出来之后,再将他们放到数组中去
    [self.arrayLb insertObject:imageF atIndex:0];
    [self.arrayLb addObject:imageL];
    
    self.myScrollView.contentSize = CGSizeMake((self.arrayLb.count + 2) * SCREEN_WIDTH, 0.15 * SCREEN_HEIGHT);
    
    self.myScrollView.contentOffset = CGPointMake(0, 0);
    
    
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0.72 * SCREEN_WIDTH, 0.115 * SCREEN_HEIGHT, 0.27 * SCREEN_WIDTH, 0.03 * SCREEN_HEIGHT)];
    self.pageControl.numberOfPages = self.arrayLb.count - 2;
    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    [self addSubview:self.pageControl];
    
    //添加方法
    [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
    
    
    
    //默认显示第一张
    [self.myScrollView scrollRectToVisible:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, 0.15 * SCREEN_HEIGHT) animated:NO];
    
    //记录当前页码为0;
    self.index = 0;
    
    
    
    
    
}

//UIPageControl方法
- (void)pageAction:(UIPageControl *)page{
    
    [self.myScrollView setContentOffset:CGPointMake((page.currentPage + 1) * self.myScrollView.frame.size.width, 0) animated:YES];
}

//重新数组的get方法
-(void)setArrayLb:(NSMutableArray<LzwBnimationLBModel *> *)arrayLb{
  
    _arrayLb = arrayLb;
    
    [self creatSubViews];
}
//定时器
- (void)TimerAction:(NSTimer *)time{
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.myScrollView.contentOffset = CGPointMake((self.index + 1) * self.myScrollView.frame.size.width, 0);
        
        
    }];
    self.index ++;
    //如果滑到最后一页;
    if (self.myScrollView.contentOffset.x == (self.arrayLb.count - 1) * SCREEN_WIDTH) {
        //就移动到第一页
        [self.myScrollView setContentOffset:CGPointMake(self.myScrollView.frame.size.width, 0)];
        //记录页码
        self.index = 1;
    }
    
}

//当手指触摸屏幕时,让计时器停止
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self.myTimer invalidate];
    
    self.myTimer = nil;
}
//当手指离开屏幕是启动计时器
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    //重新启动计时器
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(TimerAction:) userInfo:nil repeats:YES];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (self.myScrollView == scrollView) {
        //每次简述都让scrollerView所有子视图缩放为一
        for (UIScrollView *scroll in scrollView.subviews) {
            if ([scroll isKindOfClass:[UIScrollView class]]) {
                scroll.zoomScale = 1.0;
            }
        }
        if (self.myScrollView.contentOffset.x > self.myScrollView.frame.size.width * (self.arrayLb.count - 1)) {
            //循环滚动
            [self.myScrollView setContentOffset:CGPointMake(self.myScrollView.frame.size.width, 0) animated:NO];
        }
        if (self.myScrollView.contentOffset.x <= 0) {
            
            [self.myScrollView setContentOffset:CGPointMake(self.myScrollView.frame.size.width * (self.arrayLb.count - 2), 0) animated:NO];
            
        }
    }
    
    //不管怎么移动,保证存的是当前的页码
    self.index = self.myScrollView.contentOffset.x / self.myScrollView.frame.size.width - 1;
    
}
//使页面上的点的变化与视图变化一直
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    self.pageControl.currentPage = (scrollView.contentOffset.x - self.myScrollView.frame.size.width) / scrollView.frame.size.width;
    
    
}

- (void)ButtonUpAction:(UIButton *)sender{
    
    if (sender.tag == 645001) {
        //(@"点击放送表");
        [self.myDelegate broadcastButtonAction];
    }else{
        //(@"点击索引");
        [self.myDelegate searchButtonAction];
    }
    
    
    
}




//
//- (void)ImageTapAction:(UITapGestureRecognizer *)tap{
//    
//    self.number = self.pageControl.currentPage + 1;
//    
//    [self.myLBDelegate pushMulistDePage:self.number url:_arrayLb[self.number].url];
//    
//}



@end
